using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NGourd.Core.Reports
{
    public interface IReportNodeVisitor
    {
        void Visit(ReportNode node);
    }

    public class NUnitReportNodeVisitor : IReportNodeVisitor
    {
        private StringBuilder builder;

        public NUnitReportNodeVisitor()
        {
            builder = new StringBuilder();
        }

        private NUnitNodeFormatter Formatter(ReportNode node)
        {
            switch (node.TargetType)
            {
                case ReportTarget.LIFECYCLE_STEP: return new FeatureNUnitNodeFormatter(node);
                case ReportTarget.FEATURE_SET: return new FeatureNUnitNodeFormatter(node);
                case ReportTarget.FEATURE: return new FeatureNUnitNodeFormatter(node);
                case ReportTarget.SCENARIO: return new ScenarioNUnitNodeFormatter(node);
                case ReportTarget.STEP: return new StepNUnitNodeFormatter(node);
                default: throw new Exception("No formatter is defined for report target: " + node.TargetType);
            }
        }

        public void Visit(ReportNode node)
        {
            builder.AppendLine(Formatter(node).OpenTag());
            foreach (var n in node.Nodes)
            {
                n.Accept(this);
            }
            builder.AppendLine(Formatter(node).CloseTag());
        }

        public string Result()
        {
            return builder.ToString();
        }
    }

}
